package cn.song.user.web;

import cn.song.user.config.PatternProperties;
import cn.song.user.pojo.User;
import cn.song.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope // 动态刷新Nacos配置 方式一 @RefreshScope注解
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${pattern.dateformat}")
    private String dataformat;

    /**
     * 动态刷新Nacos配置 方式二 @ConfigurationProperties注解代替@Value注解
     */
    @Autowired
    private PatternProperties properties;


    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id, HttpServletRequest request) {
        System.err.println("网关filters过滤器携带的请求头 Truth = " + request.getHeader("Truth"));
        return userService.queryById(id);
    }


    /**
     * 获取nacos配置中心配置的值
     **/
    @GetMapping("now")
    public String now() {
        // return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dataformat));
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }


    /**
     * 获取nacos配置中心配置的值
     **/
    @GetMapping("prop")
    public PatternProperties prop() {

        return properties;
    }
}
